<template>
	<view class="content">
		<view class="tag">
			<view class="tag-nik">
				<u-tabs :list="list" :is-scroll="false" active-color="#FF9A00" :current="current" @change="change"></u-tabs>
			</view>
		</view>
		<view class="border"></view>
		<conversations v-show="current == 0" :Refresh="Refresh"></conversations>
		<!-- <private-letter v-if="current == 0" :dataList="dataList"></private-letter> -->

		<comment v-show="current == 1" :Refresh="Refresh"></comment>

		<give-like v-show="current == 2" :Refresh="Refreshs"></give-like>
	</view>
</template>

<script>
import conversations from './components/conversations.vue'
import comment from './components/comment.vue'
import giveLike from './components/giveLike.vue'
export default {
	components:{
		conversations,
		comment,
		giveLike
	},
	data() {
		return {
			list: [{
				name: '私信'
			}, {
				name: '评论'
			}, {
				name: '云吸和赞'
			}, {
				name: '系统通知'
			}],
			current: 0,
			unreadTotal: '',
			dataList: [],
			Refresh: 1,
			Refreshs: 1
		}
	},
	onPullDownRefresh() {
		this.Refresh += 1
		setTimeout(function () {
			uni.stopPullDownRefresh();
		}, 1000);
	},
	mounted() {
		this.Refresh += 1
		let token = uni.getStorageSync('token')
		if (!token) {
			uni.navigateTo({
				url:'/myPackageA/user/login/index'
			})
			return false
		}
	},
	methods: {
		getUserList(){
			this.goeasys.im.hereNow({
				userIds:[], //每次查询最多不超过50个
				onSuccess: function (result) {
					console.log(result)
					// result示例
					// {
					//     "code": 200,
					//     "content": [
					//         {"id": "user001","data": {"name":"Mattie"}},
					//         {"id": "user002","data": {"name":"Jack"}},
					//         {"id": "user003","data": {"name":"Mike"}}"
					//     }]
					// };
					console.log("Query online user list successfully, result:\n " + JSON.stringify(result));
				},
				onFailed: function (error) { //连接失败
				console.log(error)
					console.log("Failed to mark as read, code:" + error.code + " content:" + error.content);
				}
			});
		},
		renderConversations(content){
			let dats = content.conversations || [];
			dats.forEach(item=>{   //计算消息时间什么时候发的
				let date3 = new Date(new Date()).getTime() - new Date(this.format(item.lm,'yyyy-MM-dd HH:mm:ss')).getTime(); //时间差的毫秒数
				//计算出相差天数
				var days=Math.floor(date3/(24*3600*1000))
				//计算出小时数
				var leave1=date3%(24*3600*1000)
				//计算天数后剩余的毫秒数
				var hours=Math.floor(leave1/(3600*1000))
				//计算相差分钟数
				var leave2=leave1%(3600*1000)
				//计算小时数后剩余的毫秒数
				var minutes=Math.floor(leave2/(60*1000))
				//计算相差秒数
				var leave3=leave2%(60*1000)
				//计算分钟数后剩余的毫秒数
				var seconds=Math.round(leave3/1000)
				this.dataList.push(item)
				if(days > 0){
					item.time = days+'天前'
					return false
				}
				if(hours > 0){
					item.time = hours+'小时前'
					return false
				}
				if(minutes > 0){
					item.time = minutes+'分钟前'
					return false
				}
				if(days == 0 || hours == 0 || minutes < 1){
					item.time = '刚刚'
					return false
				}

			})
			// setTimeout(()=>{
				// this.dataList = dats
			// },3000)
			let unreadTotal = content.unreadTotal;    //未读消息总数
			this.setUnreadAmount(unreadTotal);
			// this.getUserList()
		},
		format(time, format) {
			var t = new Date(time);
			var tf = function(i) {
			  return (i < 10 ? "0" : "") + i;
			};
			return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) {
			  switch (a) {
				case "yyyy":
				  return tf(t.getFullYear());
				  break;
				case "MM":
				  return tf(t.getMonth() + 1);
				  break;
				case "mm":
				  return tf(t.getMinutes());
				  break;
				case "dd":
				  return tf(t.getDate());
				  break;
				case "HH":
				  return tf(t.getHours());
				  break;
				case "ss":
				  return tf(t.getSeconds());
				  break;
				}
			})
		},
		setUnreadAmount (unreadTotal) {
			this.unreadTotal = unreadTotal;
			if(this.unreadTotal > 0) {
				uni.setTabBarBadge({
					index: 0,
					text: this.unreadTotal.toString()
				});
			}else{
				uni.removeTabBarBadge({
					index: 0
				});
			}
		},
		toServer(){
			// let self = this;//连接服务
			let userData = {
				id:	uni.getStorageSync('uid'),
				data: {"avatar":uni.getStorageSync('userData').avatar,"nickname":uni.getStorageSync('userData').nickname}
			}
			uni.setStorageSync('userImg',userData.data.avatar)
			this.goeasys.connect({
				id: userData.id, 		//pubsub选填，im必填
				data: userData.data, 		//必须是一个对象，pubsub选填，im必填，用于上下线提醒和查询在线用户列表时，扩展更多的属性
				onSuccess: function () {  //连接成功
					console.log("GoEasy connect successfully.1") //连接成功
					// var im = self.goeasys.im;
					// im.latestConversations({
					// 	onSuccess: function (result) {
					// 		console.log(3333)
					// 		console.log(result)
					// 		console.log(3333)
					// 	},
					// 	onFailed: function (error) { //获取失败
					// 		console.log("Failed to get the latest conversations, code:" + error.code + " content:" + error.content);
					// 	},
					// })
					// self.goeasys.im.on(self.GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, (content) => {
					// 	console.log(11111111)
					// 	console.log(content)
					// 	self.renderConversations(content);
					// });
					//加载会话列表
					// this.goeasys.im.latestConversations({
					// 	onSuccess: function (result) {
					// 		console.log(result)
					// 		console.log(222222222222)
					// 		let content = result.content;
					// 		this.renderConversations(content);
					// 		uni.hideLoading();
					// 	},
					// 	onFailed: function (error) {
					// 		console.log(error)
					// 		//获取失败
					// 		uni.hideLoading()
					// 		console.log("失败获取最新会话列表, code:" + error.code + " content:" + error.content);
					// 	}
					// });
				},
				onFailed: function (error) { //连接失败
					console.log("Failed to connect GoEasy, code:"+error.code+ ",error:"+error.content);
				},
				onProgress:function(attempts) { //连接或自动重连中
					console.log("GoEasy is connecting", attempts);
				}
			});
		},
		change(index) {
			if(index == 1){
				this.Refresh += 1
			}
			if(index == 2){
				this.Refreshs += 1
			}
			setTimeout(()=>{
				this.current = index;
			},100)
		}
	},
	// mounted() {
	// 	// this.toServer()
	// }
};
</script>

<style lang="scss" scoped>
	.border{
		width: 100%;
		height: 10rpx;
		background: #f2f2f2;
	}
	.tag-nik{
		width: 700rpx;
		padding-bottom: 10rpx;
	}
	.tag{
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		background: white;
	}
.content {
	width: 100%;
	height: 100vh;
	background: #f2f2f2;
	display: flex;
	flex-direction: column;
}
</style>
